
#include	"Projectile.h"

Projectile::Projectile(){
	projectilesSurface = NULL;
	fireDirection = 1;
	madeDamage = false;
}

bool Projectile::init(projectiles p, SDL_Surface* projSurface){

	if(projSurface == NULL)
		return false;

	projectileType = p;

	projectilesSurface = projSurface;

	switch(projectileType)
	{
		case PULSE:
			collisionRect.x = 3;
			collisionRect.y = 3;
			collisionRect.w = 9;
			collisionRect.h = 8;
		break;

		case LASER:
			collisionRect.x = 6;
			collisionRect.y = 4;
			collisionRect.w = 3;
			collisionRect.h = 7;
		break;

		default:
			return false;
	}

	return true;
}

void Projectile::loop(){
	
	x += speedX * FPS::fpsControl.getSpeedFactor();
	y -= speedY * FPS::fpsControl.getSpeedFactor() * fireDirection;
}

void Projectile::render(SDL_Surface* destSurface){

	switch(projectileType)
	{
		case PULSE:
			if(fireDirection == 1)
				Surface::OnDraw(destSurface, projectilesSurface, (int) x, (int) y, 0, 0, 15, 15);
			else
				Surface::OnDraw(destSurface, projectilesSurface, (int) x, (int) y, 15, 0, 15, 15);
		break;

		case LASER:
			Surface::OnDraw(destSurface, projectilesSurface, (int) x, (int) y, 30, 0, 15, 15);
		break;
	}
}

void Projectile::cleanup(){
	//Surface is not freed because it's shared
}

void Projectile::invertFireDirection(){
	fireDirection *= -1;
}

SDL_Rect Projectile::getColRect(){
	
	SDL_Rect r;

	r.x = (int) x + collisionRect.x;
	r.y = (int) y + collisionRect.y;
	r.w = collisionRect.w;
	r.h = collisionRect.h;

	return r;
}

